Skip to content

[DWARF Import] Place relocatable object addresses where the view put their sections, and import RISC-V objects - #8731

Open
jrozner wants to merge 3 commits into
Vector35:devfrom
jrozner:dwarf-relocatable-objects
Open

jrozner wants to merge 3 commits into
Vector35:devfrom
jrozner:dwarf-relocatable-objects

Conversation

@jrozner

@jrozner jrozner commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #8729
Fixes #8730

Supersedes #8654: #8653, its first fix, was fixed separately on dev (d263624, 2699a0d), and this is the other two commits rebased onto dev.

DWARF import puts functions and data variables at the wrong addresses in relocatable objects (ET_REL), and RISC-V objects built with -g don't import at all. There are two commits, one per issue; each can be reviewed on its own.

1. Place object file addresses where the view put their sections

All sections of a .o have address 0, so a DWARF address is an offset into the section of its relocation's symbol. The importer only adds the image base, so anything outside the first section lands at the wrong address. With -ffunction-sections, every function lands on the first one. On Xtensa, where .literal precedes .text, every function is 0x20 too low. Data variables at the start of .data and .bss get the same address, so one of them is lost.

For a relocatable ELF debug file, each allocated section is found in the view by its file offset: the ELF view adds it to the raw view at that offset under the same unique name it gives it in the view (sizes must match). SHT_NOBITS sections, which aren't in the raw view, are found by name when it's unique in the file. RelocationMap::add adds that section's address to relocations against its symbols, so relocated addresses, including .debug_frame, land where the view placed the section. The addresses are taken before rebasing, since post_process rebases them afterwards like any other file's. Linked files, the .gnu_debugaltlink file and dwarfdump get an empty map and are unchanged. -fcommon symbols have no section and are not relocated.

2. Apply RISC-V label-difference relocations

Any RISC-V object built with -g fails with Failed to parse DWARF: Failed to load DWARF info: Unsupported relocation type. With linker relaxation, GNU as emits ADD/SUB, SET6/SUB6, SET8/SUB8 and SET_ULEB128/SUB_ULEB128 pairs at the same offset, which object can't put in a relocation map. On RISC-V, RelocationMap::add now applies them in file order to a copy of the section data, following the psABI and binutils (perform_relocation; the SUB_ULEB128 addend is ignored, as in binutils PR31179). It errors if a ULEB128 result doesn't fit its encoding or a SET_ULEB128 isn't followed by its SUB_ULEB128. Both labels of a pair are in one section, so the difference doesn't depend on placement.

Relation to #8728

#8728 (the object 0.41 update, 73acda8) is on dev now, and this branch is rebased onto it. object 0.41 made elf::SectionFlags a newtype, so object_section_addresses matches SectionFlags::Elf { sh_flags, .. } and checks sh_flags.contains(object::elf::SHF_ALLOC). With #8728 alone, Xtensa objects built with -g import their debug info, but at the wrong addresses described above. With this PR as well, every function and data variable is on its symbol.

Testing

Built dwarf_import from this branch and from dev (e028343, which has #8728), and loaded each headless into Binary Ninja 6.1.11189-dev (same core ABI as dev) in place of the bundled importer (corePlugins.dwarfImport: false). Checked that each imported function and data variable is at the address of the ELF symbol with its name, and compared the output (DWARF functions, data variables and types, and the view's function types and variables).

  • The reproducers in DWARF import: functions and data variables of relocatable objects are placed at section offsets, not addresses #8729 and DWARF import: RISC-V objects built with -g fail with "Unsupported relocation type" #8730: with this branch, add, twice, bump, counter and zeroed are each at their ELF symbol in objs.o, objs_fs.o and objs_rv32.o.
  • x86-64 objects (gcc, -O2 -g, with and without -ffunction-sections -fdata-sections): on dev, with -ffunction-sections all 12 functions are at 0x400000. Without it, fp lands on top of the function add3 and sink is missing. With this branch, all 12 functions and both data variables are on their symbols. The same holds with loader.imageBase 0x800000 and 0x1000.
  • RISC-V: an ESP32-C3 corpus (Espressif GCC 14.2, -O2 -ggdb and -Og -ggdb) fails on dev and imports with this branch: 20 functions, 2 data variables and 4 types, all on their symbols, also at both image bases above. The DWARF function signatures, data variable types and named types are identical to the import of the same object linked with ld.
  • Xtensa (an ESP32 corpus built the same way, with an Xtensa architecture plugin that has an ELF relocation handler): on dev, the debug info imports but every function is 0x20 too low (add3 at 0x10000, its symbol at 0x10020), fp lands at the start of the view and sink is missing. With this branch, all 12 functions with an address and both data variables are on their symbols, also at both image bases above. The function signatures, types, and the view's function types and local variables are identical to the import of the same object linked with ld.
  • Unchanged: linked x86-64 PIE (also with loader.imageBase 0x800000 and 0x1000) and non-PIE binaries, and linked ESP32 and ESP32-C3 ELFs, give identical output to dev.
  • Clippy reports no warnings on the lines this PR changes, and cargo fmt --check passes.

…d their sections

Every section of a relocatable ELF object is at address 0, so an address in
its DWARF info is an offset into the section its relocation's symbol is in.
The ELF view lays those sections out one after another from the image base,
but the importer only added the image base, so everything outside the first
section landed in the wrong place: with -ffunction-sections every function
was at the image base, on Xtensa, where .literal precedes .text, every
function was 0x20 too low, and data variables at the start of .data and
.bss got the same address, so one of them was lost.

When the debug file is a relocatable ELF object, look up where the view put
each allocated section and add that to the relocations against symbols in
it. Linked files are unaffected.
…tions

Loading a RISC-V relocatable object built with -g failed with "Failed to
load DWARF info: Unsupported relocation type", so no debug info was
imported. object's RelocationMap only accepts absolute relocations, and
with linker relaxation GNU as encodes differences between code labels
as relocation pairs at the same offset: R_RISCV_ADD*/SUB* (high_pc,
line table and FDE lengths), R_RISCV_SET6/SUB6 and SET8/SUB8 (CFA
advances) and R_RISCV_SET_ULEB128/SUB_ULEB128 (location and range list
offsets).

Apply these to the section data before handing it to gimli, in order,
as binutils' perform_relocation does, following the psABI formulas. The
object isn't relaxed, so this gives the right difference. ULEB128 values
keep the length the assembler gave them, and SUB_ULEB128's addend is
ignored like binutils does. R_RISCV_NONE and R_RISCV_RELAX are skipped,
and R_RISCV_32/64 still go through the relocation map, which moves them
to where the view placed their section.

The pairs use the symbols' offsets into their sections as S, without the
section's address in the view: both labels of a pair are in the same
section, so their difference doesn't depend on where it was placed.

@ElykDeer ElykDeer left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the below comment. Now that the Object PR is merge, mind changing those flags as you suggested in this PR please? Looks like this was already done!

Comment thread plugins/dwarf/dwarf_import/src/lib.rs Outdated
Comment on lines +562 to +568
// The ELF view keeps the first section with a given name as is, and suffixes the
// name of each later one with `#` and its section index.
let view_name = if seen_names.insert(name) {
name.to_string()
} else {
format!("{name}#{}", section.index().0)
};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's an API you can call which will generate these names for you: BNGetUniqueSectionNames/BinaryView::GetUniqueSectionNames. I think our internal implementation results in slightly messier names than this does, see this binary (8731-section-name-collision.zip), which has two sections named .demo and one named .demo#5. Comes out as

image

(on the right: two sections referencing the +38 offset into the string table on the left for .demo, and the bottom one on the right referencing +0xb1 for .demo#5. Results in the names .demo, .demo#5, and .demo#5#6 in the top left)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually thinking about it more, you don't want to call that API. You can probably just match the index of the section you want and pull that same index from our section list. I'd like a better solution.....but those lists "shouldn't" get out of order atm

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 9008093. Good catch, thanks for the repro. Agreed on not calling GetUniqueSectionNames after the fact. I went with matching by file offset instead of by index, since the view's section list leaves out non-allocated, NOTE and empty sections and adds .extern/.common/etc., so its indices don't line up with the ELF section table. The ELF view adds each allocated section with contents to the raw parent view at its sh_offset, under the same unique name, so the importer now looks up the section's file range there, takes that name, and finds the section in the view by it (the size check stays). SHT_NOBITS sections aren't in the raw view, so they fall back to the name only when no other section in the file has it. That doesn't depend on how the names are generated or on the order of the section list. On your vars-collision.o, first, second and third are each at their symbol now (before, third landed on second), and the rest of the test corpus imports exactly as before.

… guessed name

The ELF view names duplicate sections with GetUniqueSectionNames, whose
names can't be predicted from the file: sections named .demo, .demo and
.demo#5 become .demo, .demo#5 and .demo#5#6, so guessing <name>#<index>
found the wrong section for the third one.

The ELF view also adds each allocated section with contents to the raw
view at its file offset, under the same name. Find the name there by the
section's file range instead. Sections without contents (SHT_NOBITS)
aren't in the raw view, so they are found by name only when no other
section in the file has it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants